package com.company.runner;

import com.company.statistician.Column;
import com.company.statistician.noise.LaplaceNoiser;
import com.company.statistician.noise.Noiser;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class AddNoiseRunner {
    public static void main(String[] args) {
        int aveTimes = 64;
        int lumbda = 128;
        System.out.println("Before Add Noise........");
        Noiser noiser = new LaplaceNoiser(1, 2 * lumbda + 1);
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet();
            ArrayList<Column> befColumns = LoadColumns(DataSet.OutputPath.BASE);
            Column.showValues(befColumns, sheet, 0);
            for (int i = 1; i <= aveTimes; i++) {
                ArrayList<Column> aftColumns = noiser.addNoise(befColumns);
                Column.showValues(aftColumns, sheet, i);
            }
            workbook.write(new FileOutputStream(DataSet.OutputPath.BASE));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static ArrayList<Column> LoadColumns(String columnPath) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(columnPath);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        ArrayList<Column> columns = new ArrayList<>();
        for (int i = 0; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell cell = row.getCell(0);
            double numericCellValue = cell.getNumericCellValue();
            columns.add(new Column(0, 0, numericCellValue));
        }
        return columns;
    }

}
